/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fiap.controller;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author pf0591
 */
@WebFilter(urlPatterns="/*")
public class FiltroSessao implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpSession session = req.getSession();
        Boolean logado = (Boolean) session.getAttribute("logado"); 
        
        if( logado !=null && logado.booleanValue() ){
            chain.doFilter(request, response);
        } else { 
            req.getRequestDispatcher("/logar").forward(request, response);
        } 
            
    }

    @Override
    public void destroy() {
        
    }
    
    
}
